package com.example.wms;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.google.mlkit.vision.barcode.BarcodeScannerOptions;
import com.google.mlkit.vision.barcode.common.Barcode;
import com.google.mlkit.vision.common.InputImage;
import com.kathline.barcode.BitmapUtils;
import com.kathline.barcode.CameraImageGraphic;
import com.kathline.barcode.CameraSourcePreview;
import com.kathline.barcode.FrameMetadata;
import com.kathline.barcode.GraphicOverlay;
import com.kathline.barcode.MLKit;
import com.kathline.barcode.barcodescanner.WxGraphic;

import java.nio.ByteBuffer;
import java.util.List;

public class QRCodeActivity extends AppCompatActivity implements View.OnClickListener {
    MLKit mlKit;
    private CameraSourcePreview preview;
    private GraphicOverlay graphicOverlay;

    private ImageView imgBack;
    private ImageView imgSwitchCamera;
    private RelativeLayout bottomMask;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qrcode);

        // init view
        imgBack = findViewById(R.id.img_back);
        imgBack.setOnClickListener(this);
        imgSwitchCamera = findViewById(R.id.img_switch_camera);
        imgSwitchCamera.setOnClickListener(this);
        bottomMask = findViewById(R.id.bottom_mask);

        //构造出扫描管理器
//        configViewFinderView(viewfinderView);
        preview = findViewById(R.id.preview_view);
        graphicOverlay = findViewById(R.id.graphic_overlay);
        mlKit = new MLKit(this, preview, graphicOverlay);
        //是否扫描成功后播放提示音和震动
        mlKit.setPlayBeepAndVibrate(true, true);
        //仅识别二维码
        BarcodeScannerOptions options =
                new BarcodeScannerOptions.Builder()
                        .setBarcodeFormats(
                                Barcode.FORMAT_QR_CODE,
                                Barcode.FORMAT_AZTEC)
                        .build();
        mlKit.setBarcodeFormats(null);
        mlKit.setOnScanListener(new MLKit.OnScanListener() {
            @Override
            public void onSuccess(List<Barcode> barcodes, @NonNull GraphicOverlay graphicOverlay, InputImage image) {
                showScanResult(barcodes, graphicOverlay, image);
            }

            @Override
            public void onFail(int code, Exception e) {

            }
        });
    }
    private void showScanResult(List<Barcode> barcodes, @NonNull GraphicOverlay graphicOverlay, InputImage image) {
        if (barcodes.isEmpty()) {
            return;
        }
        Bitmap bitmap = null;
        ByteBuffer byteBuffer = image.getByteBuffer();
        if (byteBuffer != null) {
            FrameMetadata.Builder builder = new FrameMetadata.Builder();
            builder.setWidth(image.getWidth())
                    .setHeight(image.getHeight())
                    .setRotation(image.getRotationDegrees());
            bitmap = BitmapUtils.getBitmap(byteBuffer, builder.build());
        } else {
            bitmap = image.getBitmapInternal();
        }
        if (bitmap != null) {
            graphicOverlay.add(new CameraImageGraphic(graphicOverlay, bitmap));
        }
        for (int i = 0; i < barcodes.size(); ++i) {
            Barcode barcode = barcodes.get(i);
            WxGraphic graphic = new WxGraphic(graphicOverlay, barcode);
            graphic.setColor(getResources().getColor(R.color.colorAccent));
            Bitmap bitmapPaint = BitmapFactory.decodeResource(getResources(), R.mipmap.ico_wechat);
            graphic.setBitmap(bitmapPaint);
            graphic.setOnClickListener(new WxGraphic.OnClickListener() {
                @Override
                public void onClick(Barcode barcode) {
//                    Toast.makeText(getApplicationContext(), "圆被点击: " + barcode.getRawValue(), Toast.LENGTH_SHORT).show();
                    Intent data = new Intent();
                    data.putExtra("value",barcode.getRawValue());
                    setResult(1,data);
                    finish();
                }
            });
            graphicOverlay.add(graphic);
        }
        if (barcodes.size() > 0) {
            imgBack.setVisibility(View.VISIBLE);
            imgSwitchCamera.setVisibility(View.INVISIBLE);
            bottomMask.setVisibility(View.GONE);
            mlKit.stopProcessor();
        }




    }

    @Override
    public void onClick(View view) {

    }
}